package com.txx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.txx.mapper.GoodsCategoryMapper;
import com.txx.pojo.GoodsCategory;
import com.txx.pojo.Permission;
import com.txx.pojo.User;
import com.txx.util.Constants;
import com.txx.vof.GoodsCategoryForm;
import com.txx.vor.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: zgc
 * @Date: 2021/5/19 9:37
 */

@Service
public class GoodsCategoryServiceImpl implements  GoodsCategoryService{

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

//    @Override
//    public List<GoodsCategory> getGoodsCategory(Integer type) {
//
//        List<GoodsCategory> allList = goodsCategoryMapper.selectList(null);
//
//        // 获取第一级的菜单
//        List<GoodsCategory> oneList = allList.stream()
//                .filter(e -> e.getLevel() == (Integer) Constants.GoodsCategoryLevel.One.ordinal())
//                .collect(Collectors.toList());
//
//        // 组装成两层
//        List<GoodsCategory> list = new ArrayList<GoodsCategory>();
//        for(GoodsCategory oneGc : oneList)
//        {
//            GoodsCategory gc = oneGc;
//            List<GoodsCategory> newList = new ArrayList<>();
//            List<GoodsCategory>  twoList = allList.stream().filter(e -> e.getParentId() == oneGc.getId()).collect(Collectors.toList());
//            for (GoodsCategory twoGc : twoList)
//            {
//                List<GoodsCategory> threeList = allList.stream().filter(e -> e.getParentId() == twoGc.getId()).collect(Collectors.toList());
//                twoGc.setChildren(threeList);
//
//                newList.add(twoGc);
//            }
//
//            System.out.println(gc);
//            gc.setChildren(newList);
//            list.add(gc);
//        }
//        return list;
//    }

    //问题: 代码与数据库交互频繁必须优化
    //思路: 1.只查询一次数据库
    //      2.Map<parentId,List<ItemCat>>
    public Map<Integer,List<GoodsCategory>> getMap(){
        Map<Integer,List<GoodsCategory>> map = new HashMap<>();
        //1.查询所有的数据

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.ne("level",Constants.GoodsCategoryLevel.One.ordinal());

        List<GoodsCategory> itemCatList =
                goodsCategoryMapper.selectList(queryWrapper);
        //2.封装Map集合数据
        //2.1存储依据: 如果key存在?获取子级之后add操作
        //2.2         如果key不存在 则将key存储,同时将自己当做第一个元素保存
        for (GoodsCategory itemCat : itemCatList){
            int parentId = itemCat.getParentId();
            //判断map集合中是否有父级
            if(map.containsKey(parentId)){
                //有父级 将自己添加到子级中
                map.get(parentId).add(itemCat);
            }else{
                List<GoodsCategory> list = new ArrayList<>();
                list.add(itemCat);
                map.put(parentId,list);
            }
        }
        return map;
    }

    @Override
    public PageResult getGoodsCategory(GoodsCategoryForm data) {

        if(data.getType() == null){
            data = new GoodsCategoryForm(){{
                setPage(1);
                setType(3);
                setPageSize(1000000);
            }};
        }
        if(data.getPage() == null || data.getPageSize() == null)
        {
            data.setPage(1);
            data.setPageSize(1000000);
        }

        Page<GoodsCategory> oneList = findOneItemCatList(data);
        Map<Integer,List<GoodsCategory>> map = getMap();
        if(data.getType() == 1)
            return new PageResult(){{setTotalCount(oneList.getTotal());}};
        if(data.getType() == 2)
            return  new PageResult(){{setTotalCount(oneList.getTotal());setList(findTwoItemCatList(map,oneList.getRecords()));}};;
        return new PageResult(){{setTotalCount(oneList.getTotal());setList(findThreeItemCatList(map,oneList.getRecords()));}};
    }

    public Page<GoodsCategory> findOneItemCatList(GoodsCategoryForm data)
    {
        // 第1个参数：查询第几页 第2个参数：每页几条数据
        Page<GoodsCategory> page = new Page<>(data.getPage(), data.getPageSize());

        //2.定义查询条件
        QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level",Constants.GoodsCategoryLevel.One.ordinal());
        //3.判断用户是否有参数
       return goodsCategoryMapper.selectPage(page, queryWrapper);
    }

    public List<GoodsCategory> findTwoItemCatList(Map<Integer,List<GoodsCategory>> map,List<GoodsCategory> oneList){

        //2.根据一级查询二级
        for(GoodsCategory itemCat : oneList){
            List<GoodsCategory> twoList = map.get(itemCat.getId());
            itemCat.setChildren(twoList);
        }
        return oneList;
    }

    private List<GoodsCategory> findThreeItemCatList(Map<Integer,List<GoodsCategory>> map,List<GoodsCategory> list) {
        List<GoodsCategory> oneList = findTwoItemCatList(map,list);
        for (GoodsCategory itemCat : oneList){    //1级菜单
            if(itemCat.getChildren() !=null){
                for(GoodsCategory itemCat2 :itemCat.getChildren()){
                    List<GoodsCategory> threeList = map.get(itemCat2.getId());
                    itemCat2.setChildren(threeList);
                }
            }
        }
        return oneList;
    }

    @Override
    public int insert(GoodsCategory goodsCategory) {
        goodsCategory.setImg("");
        goodsCategory.setSort(1);
        goodsCategory.setIsDelete(Constants.IsDelete.Off.ordinal());
        goodsCategory.setUpdated(new Date());
        goodsCategory.setCreated(new Date());
        goodsCategory.setStatus(Constants.Status.No.ordinal()== 0?false:true);
        return goodsCategoryMapper.insert(goodsCategory);
    }

    @Override
    public GoodsCategory getCategoryById(int id) {
        return goodsCategoryMapper.selectById(id);
    }

    @Override
    public int Update(GoodsCategory goodsCategory)
    {
        return goodsCategoryMapper.updateById(goodsCategory);
    }

    @Override
    public int delete(int id) {
        return goodsCategoryMapper.deleteById(id);
    }
}
